package cn.com.homeyoda.aimate.aihealth.ui.health.frragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.bean.SportPlanBean;
import cn.com.homeyoda.aimate.aihealth.bean.SportPlanTitleBean;
import cn.com.homeyoda.aimate.aihealth.ui.health.SportPlanActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.SportPlanDetailsActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.adapter.SportPlanAdapter;
import cn.com.homeyoda.aimate.aihealth.ui.health.adapter.SportPlanTitleAdapter;
import cn.com.homeyoda.aimate.aihealth.views.SpaceItemDecoration;

public class SportPlanFragment extends Fragment {

    private View rootView;

    private SportPlanActivity sportPlanActivity;
    private SportPlanTitleAdapter sportPlanTitleAdapter;
    private SportPlanAdapter sportPlanAdapter;

    private RecyclerView rv_plan_bar, rv_plan_content;

    List<SportPlanTitleBean.DataBean> planTitleBeans = new ArrayList<>();
    List<SportPlanBean.RowsBean> planBeans = new ArrayList<>();

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        sportPlanActivity = (SportPlanActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_sport, container, false);
            /**
             * 我的运动计划
             */
            rootView.findViewById(R.id.btn_my_plan).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sportPlanActivity.changeMyPlanFragment();
                }
            });

            rv_plan_bar = rootView.findViewById(R.id.rv_plan_bar);
            rv_plan_content = rootView.findViewById(R.id.rv_plan_content);
            initRecyclerView();
        }


        return rootView;
    }


    private void initRecyclerView() {

        sportPlanTitleAdapter = new SportPlanTitleAdapter(R.layout.item_sport_plan_title, planTitleBeans, sportPlanActivity);
        rv_plan_bar.setLayoutManager(new LinearLayoutManager(sportPlanActivity, LinearLayoutManager.HORIZONTAL, false));
        rv_plan_bar.setAdapter(sportPlanTitleAdapter);
        sportPlanTitleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                sportPlanTitleAdapter.setSelectPosition(position);
                sportPlanActivity.querySportPlans(sportPlanTitleAdapter.getData().get(position).getTypeValue());
            }
        });


        sportPlanAdapter = new SportPlanAdapter(R.layout.item_sport_plan, planBeans, sportPlanActivity);

        rv_plan_content.addItemDecoration(new SpaceItemDecoration(0, 0, 20, 20));
        rv_plan_content.setLayoutManager(new GridLayoutManager(sportPlanActivity, 2));
        rv_plan_content.setAdapter(sportPlanAdapter);

        sportPlanAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("planId", sportPlanAdapter.getData().get(position).getId());
                bundle.putString("faceId",sportPlanActivity.getFaceId());
                sportPlanActivity.startActivity(SportPlanDetailsActivity.class, bundle);
            }
        });
    }


    public void updateTitle(List<SportPlanTitleBean.DataBean> planTitleBeans) {
        this.planTitleBeans = planTitleBeans;
        if (sportPlanTitleAdapter != null) {
            sportPlanTitleAdapter.setNewData(planTitleBeans);
        }
    }

    public void upDateContent(List<SportPlanBean.RowsBean> planBeans) {
        this.planBeans = planBeans;
        if (sportPlanAdapter != null) {
            sportPlanAdapter.setNewData(planBeans);
        }
    }
}
